<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	/*
 *      Media.php
 *      
 *      Copyright 2011 godfather.typhoon.tycoon@banme <chungnguyenhuu911@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 *      
 *      
 */

	class  SubSlide   extends  CI_Controller{
		private $data = array(
				'total' => 0,
				'images' => array(),
				'error' => ''
			);
		function  SubSlide  (){
			
			}
		function __construct(){
			parent::__construct();
			$this->load->library('pagination');
			$this->lang->load('messages','vietnamese'); 
			$this->lang->load('upload','vietnamese'); 
			/* ---------------------------------------------------
			 * Check admin login
			 *  
			*/ 

			if(!$this->session->userdata('go_admin')){
				redirect('admin/login', 'refesh');
				return;
			}
		}
		function index($start = 0){			
			if ($this->input->post('btn_upload')) {
				$file_name = $this->upload();
				if($file_name != null){
					redirect('subslide/index/', 'refesh');
				}
			}
			
			$this->data['dir'] = $this->config->item('sub_slide.path');
			
			$config['base_url'] = site_url('subslide/index/');
			$config['per_page'] = $this->config->item('admin.product.video.page');
			$config['uri_segment'] = 3;
			$finish = $start + $config['per_page'];
			/*
			* readdir image
			*/
			if (is_dir($this->data['dir']))
			{
				
				$i = 0;
				if ($dh = opendir($this->data['dir'])) {
					while (($file = readdir($dh)) !== false) {
						// get file extension
						$ext = strrev(strstr(strrev($file), ".", TRUE));
						if ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'png') {
							if ($start <= $this->data['total'] && $this->data['total'] < $finish) {
								//$this->data['images'][$i]['name'] = $file;
								$this->data['images'][] = $file;
								$i++;
							}
							$this->data['total']++;
						}
					}
					closedir($dh);
				}
			}
			
			$config['total_rows'] = $this->data['total'];
			
			$this->pagination->initialize($config);
			
			
			
			$this->data['main_content'] = 'manager/subslide';
			$this->load->view('templates/manager/template', $this->data);
			//return;
			
		}
		function config_upload_file($path = "", $allowed_types = "", $max_size = -1, $max_width = -1, $max_height = -1, $override = FALSE){
			$config['upload_path'] = $path;
			$config['allowed_types'] = $allowed_types;
			$config['max_size']	= $max_size;
			$config['max_width']  = $max_width;
			$config['max_height']  = $max_height;
			$config['overwrite']  = $override;
			return $config;
		}
		
		private function upload()
		{
			$file_name = null;
			$c_upload['upload_path']    = $this->config->item('sub_slide.path');
			$c_upload['allowed_types']  = 'gif|jpg|png|jpeg|x-png';
			$c_upload['max_size']       = '500';
			$c_upload['max_width']      = '1600';
			$c_upload['max_height']     = '1200';
			$c_upload['remove_spaces']  = TRUE;
			
			$this->load->library('upload', $c_upload);
			if ($this->upload->do_upload()) {
				$img = $this->upload->data();
				$file_name =  $img['file_name'];
			} else {
				$this->data['error'] = $this->upload->display_errors();
			}
			return $file_name;
		}

		public function delete($name)
		{
			$this->removieFile($this->config->item('sub_slide.path'),$name);
			redirect('slide/index');
			return;
		}
		
		function removieFile($path_file, $name){
			$path = $path_file . $name;
			if(file_exists($path)){
				unlink($path);
			}
		}
		
	}
?>
